package com.wu.common.utils;

import com.google.gson.Gson;
import com.wu.entity.weixin.po.WeixinReqConfig;
import com.wu.common.handel.WeiXinReqHandler;
import com.wu.common.handel.WeiXinReqService;
import com.wu.entity.weixin.requ.WeixinReqParam;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;

public class WeiXinReqUtil {
    private static Logger logger = LoggerFactory.getLogger(WeiXinReqUtil.class);
    private static Map<String, WeixinReqConfig> REQ_MAPPING = new HashMap();
    private static Map<String, WeiXinReqHandler> MAPPING_HANDLER = new HashMap();
    private static Properties file_content_type = null;

    public WeiXinReqUtil() {
    }

    public static Object getObjectByClassName(String className) {
        Class obj = null;

        try {
            obj = Class.forName(className);
            return obj.newInstance();
        } catch (ClassNotFoundException var3) {
            var3.printStackTrace();
        } catch (InstantiationException var4) {
            var4.printStackTrace();
        } catch (IllegalAccessException var5) {
            var5.printStackTrace();
        }

        return obj;
    }

    public static void initReqConfig(String configName) throws JDOMException, IOException {
        InputStream is = WeiXinReqService.class.getClassLoader().getResourceAsStream(configName);
        SAXBuilder xmlBuilder = new SAXBuilder();
        Document doc = xmlBuilder.build(is);
        Element objRoot = doc.getRootElement();
        List<Element> lstMapping = objRoot.getChildren("req");
        WeixinReqConfig objConfig = null;
        Iterator var7 = lstMapping.iterator();

        while(var7.hasNext()) {
            Element mapping = (Element)var7.next();
            String key = mapping.getAttribute("key").getValue();
            String method = mapping.getAttribute("method").getValue();
            String url = mapping.getAttribute("url").getValue();
            String mappingHandler = "org.jeewx.api.core.handler.impl.WeixinReqDefaultHandler";
            String datatype = WeiXinConstant.PARAM_DATA_TYPE;
            if (mapping.getAttribute("mappingHandler") != null) {
                mappingHandler = mapping.getAttribute("mappingHandler").getValue();
            }

            if (mapping.getAttribute("datatype") != null) {
                datatype = mapping.getAttribute("datatype").getValue();
            }

            objConfig = new WeixinReqConfig();
            objConfig.setKey(key);
            objConfig.setMappingHandler(mappingHandler);
            objConfig.setMethod(method);
            objConfig.setUrl(url);
            objConfig.setDatatype(datatype);
            registerMapping(key, objConfig);
        }

    }

    public static void registerMapping(String key, WeixinReqConfig weixinReqConfig) {
        REQ_MAPPING.put(key, weixinReqConfig);
    }

    public static WeixinReqConfig getWeixinReqConfig(String key) {
        return (WeixinReqConfig)REQ_MAPPING.get(key);
    }

    public static WeiXinReqHandler getMappingHander(String className) {
        WeiXinReqHandler handler = (WeiXinReqHandler)MAPPING_HANDLER.get(className);
        if (handler == null) {
            handler = (WeiXinReqHandler)getObjectByClassName(className);
            MAPPING_HANDLER.put(className, handler);
        }

        return handler;
    }

    public static Map getWeixinReqParam(WeixinReqParam weixinReqParam) {
        Gson gson = new Gson();
        Object params = null;

        try {
            String json = gson.toJson(weixinReqParam);
            params = (Map)gson.fromJson(json, Map.class);
        } catch (Exception var4) {
            logger.error("处理参数解析出错", var4);
            params = new HashMap();
            ((Map)params).put("access_token", weixinReqParam.getAccess_token());
        }

        return (Map)params;
    }

    public static String getWeixinParamJson(WeixinReqParam weixinReqParam) {
        Gson gson = new Gson();
        String json = gson.toJson(weixinReqParam);
        return json;
    }

    public static String getFileContentType(String fileSuffix) {
        if (file_content_type == null) {
            file_content_type = new Properties();
            InputStream in = WeiXinReqService.class.getClassLoader().getResourceAsStream("fie-content-type.properties");

            try {
                file_content_type.load(in);
                in.close();
            } catch (IOException var3) {
                var3.printStackTrace();
                file_content_type = new Properties();
            }
        }

        return (String)file_content_type.get(fileSuffix);
    }

    public static String getFileSuffix(String contentType) {
        if (file_content_type == null) {
            file_content_type = new Properties();
            InputStream in = WeiXinReqService.class.getClassLoader().getResourceAsStream("fie-content-type.properties");

            try {
                file_content_type.load(in);
                in.close();
            } catch (IOException var5) {
                var5.printStackTrace();
            }
        }

        String fileSuffix = "";
        Set type = file_content_type.entrySet();
        Iterator it = type.iterator();

        while(it.hasNext()) {
            Entry entity = (Entry)it.next();
            if (entity.getValue().equals(contentType)) {
                fileSuffix = (String)entity.getKey();
                break;
            }
        }

        return fileSuffix;
    }

    public static void main(String[] args) throws JDOMException, IOException {
        String realPath = WeiXinReqUtil.class.getClassLoader().getResource("").getFile();
        System.out.println(realPath);
        initReqConfig("weixin-reqcongfig.xml");
    }
}

